package prassein.rsm.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.beans.factory.annotation.Autowired;

import prassein.rsm.common.utils.Constants;
import prassein.rsm.dto.DownloadChoice;
import prassein.rsm.dto.ResumeDto;
import prassein.rsm.entity.base.Resume;
import prassein.rsm.service.base.AuditManager;
import prassein.rsm.service.base.ResumeManager;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Results({
	@Result(name = "success",location="resumeManager.jsp"), 
	@Result(name = "error",location="login.jsp"),
	@Result(name = "input",location="resumeInsert.jsp")})
public class ResumeManagerAction extends ActionSupport implements ServletContextAware{
	
	private static SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
	
	@Autowired
	private ResumeManager resumeManager;
	
	@Autowired
	private AuditManager auditManager;
	
	private static final long serialVersionUID = -7416211125815929519L;
	
	private ServletContext context;
	
	private long resumeId = -1;
	
	public int totalCount = 0;
	
	private int isDeleteMode;
	private String deleteString;
	
	private List<ResumeDto> resumeList;
	
	private DownloadChoice filter = new DownloadChoice();
	
	public List<ResumeDto> getResumeList() {
		return resumeList;
	}
	public void setResumeList(List<ResumeDto> resumeList) {
		this.resumeList = resumeList;
	}
	
	@Override 
	public String execute() throws Exception {
		initEvn();
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get(Constants.USERNAME) != null && !session.get(Constants.USERNAME).equals("")) {
			String res = SUCCESS;
			
			int state = 0;
			String stateString = "跟进中";
			if (isDeleteMode == 1) {
				state = Constants.DELETED;
				stateString = "删除";
			} else if (isDeleteMode == 2){
				state = Constants.CLOSE;
				stateString = "关闭";
			}
			
			if (isDeleteMode > 0) {
				String[] ids = deleteString.split(";");
				if (ids != null) {
					for (int i = 0; i < ids.length; i++) {
						Resume resume1 = resumeManager.getResume(Integer.parseInt(ids[i]));
						if (resume1 != null) {
							resume1.setState(state);
							resumeManager.save(resume1);
							
							auditManager.save(session.get(Constants.USERNAME).toString(), resume1.getName(), "修改状态为:" + stateString);
						}
					}
				}
			}
			
			res = ListAllResume(session);
			cleanEvn();
			return res;
		} else {
			cleanEvn();
			return ERROR;
		}
	}
	
	private String ListAllResume(Map<String, Object> session) {
		List<Resume> resumeListTemp = resumeManager.getAllResumes();
		resumeList = new ArrayList<ResumeDto>();
		totalCount = resumeListTemp.size();
		
		java.util.Date date = new Date();
		for (Resume item : resumeListTemp) {
			try {
				item.setDateOfBirth(GetAge(date, item));
				ResumeDto dto = new ResumeDto();
				if (item.getState() == null) {
					dto.setState("N/A");
				} else if (item.getState() == 0) {
					dto.setState("进行中");
				} else if (item.getState() == 1) {
					dto.setState("关闭");
				} else if (item.getState() == 2) {
					dto.setState("已删除");
				} else {
					dto.setState("N/A");
				}
				
				dto.setResume(item);
				resumeList.add(dto);
			} catch (Exception e) {
				System.out.print(e);
			}
		}
		session.remove(Constants.LATEST_EDIT_REUMEID);
		
		return SUCCESS;
	}
	
	private String GetAge(java.util.Date date, Resume item) throws ParseException {
		try {
			java.util.Date mydate = myFormatter.parse(item.getDateOfBirth());
			long day = (date.getTime()-mydate.getTime())/(24*60*60*1000) + 1;
			String year = new java.text.DecimalFormat("#").format(day/365f);
			return year + "岁";
		} catch(Exception e) {
			return "0岁";
		}
	}
	
	private void initEvn() {
	}
	private void cleanEvn() {
		resumeId = -1;
		isDeleteMode = 0;
		deleteString = "";
	}
	
	public ResumeManager getResumeManager() {
		return resumeManager;
	}
	public void setResumeManager(ResumeManager resumeManager) {
		this.resumeManager = resumeManager;
	}
	public long getResumeId() {
		return resumeId;
	}
	public void setResumeId(long resumeId) {
		this.resumeId = resumeId;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	@Override
	public void setServletContext(ServletContext context) {
		this.context = context;
	}
	public DownloadChoice getFilter() {
		return filter;
	}
	public void setFilter(DownloadChoice filter) {
		this.filter = filter;
	}
	public AuditManager getAuditManager() {
		return auditManager;
	}
	public void setAuditManager(AuditManager auditManager) {
		this.auditManager = auditManager;
	}
	public int getIsDeleteMode() {
		return isDeleteMode;
	}
	public void setIsDeleteMode(int isDeleteMode) {
		this.isDeleteMode = isDeleteMode;
	}
	public String getDeleteString() {
		return deleteString;
	}
	public void setDeleteString(String deleteString) {
		this.deleteString = deleteString;
	}
}
